package com.redlight.base;

import com.google.common.collect.Maps;
import com.redlight.pool.PoolArgs;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.redlight.utils.StringUtil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chao on 2019/1/9.
 */
public class Global {
    private static Logger LOGGER = LoggerFactory.getLogger(Global.class);
//    private static Properties properties = loadProperties(new String[]{"com/redlight/config/system.properties", "com/redlight/config/jdbc.properties", "com/redlight/config/app.properties"});
    private static Properties properties =null;
    private static Map<String, String> map = Maps.newHashMap();
    public static final String USER = "user";
    public static final String SHOW = "1";
    public static final String HIDE = "0";
    public static final String YES = "1";
    public static final String NO = "0";
    public static final String TRUE = "true";
    public static final String FALSE = "false";
    public static final String DEL_FLAG_NORMAL = "0";
    public static final String DEL_FLAG_DELETE = "1";
    public static final String RET_MSG_NAME = "message";
    public static final String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";
    public static final String USERFILES_BASE_URL = "/static/common/userfiles/";
    public static  Map<String,PoolArgs> poolArgMap=new HashMap<>();
    public Global() {
    }

    public static String getConfig(String key) {
        if(key == null) {
            return "";
        } else {
            boolean noExists = false;
            String value = (String)map.get(key);
            if(value == null) {
                value = properties.getProperty(key);
                noExists = true;
            }

            if(value != null && value.indexOf("${") > -1) {
                String valueTmp = value;
                Pattern p = Pattern.compile("\\$\\{(.*?)}");

                String param;
                String paramName;
                for(Matcher m = p.matcher(value); m.find(); valueTmp = valueTmp.replace(param, getConfig(paramName))) {
                    param = m.group();
                    paramName = m.group(1);
                }

                value = valueTmp;
            }

            if(noExists) {
                map.put(key, value != null?value:"");
            }

            return value != null?value:"";
        }
    }

    public static String getUrlSuffix() {
        return getConfig("urlSuffix");
    }

    public static String getDbType() {
        return getConfig("jdbc.type");
    }

    public static String getProductName() {
        return getConfig("productName");
    }

    public static String getMenuModule() {
        return getConfig("module.menu");
    }

    public static String getTitle() {
        return "欢迎使用" + getProductName();
    }

    public static Boolean isDemoMode() {
        String dm = getConfig("demoMode");
        return Boolean.valueOf("true".equals(dm) || "1".equals(dm));
    }

    public static Boolean isSynActivitiIndetity() {
        String dm = getConfig("activiti.isSynActivitiIndetity");
        return Boolean.valueOf("true".equals(dm) || "1".equals(dm));
    }

    public static Object getConst(String field) {
        try {
            return Global.class.getField(field).get((Object)null);
        } catch (Exception var2) {
            return null;
        }
    }

    public static String getUserfilesBaseUrl(boolean contextPath) {
        String url = "/static/common/userfiles/";
        if(contextPath) {
            try {
                return WebUtil.getServletContext().getContextPath() + url;
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return url;
    }

    public static String getUserfilesBaseDir() {
        String dir = getConfig("userfiles.basedir");
        if(StringUtils.isBlank(dir)) {
            try {
                dir = WebUtil.getServletContext().getRealPath("/");
            } catch (Exception var2) {
                return "";
            }
        }

        if(StringUtil.isNotBlank(dir)) {
            dir = FileUtil.getFilePathByOS(dir);
        }

        return dir + "/static/common/userfiles/";
    }

    public static Properties loadProperties(String... resourcesPaths) {
        Properties props = new Properties();
        String[] var2 = resourcesPaths;
        int var3 = resourcesPaths.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String location = var2[var4];
            Reader reader = null;

            try {
                reader = Resources.getResourceAsReader(location);
                props.load(reader);
            } catch (IOException var11) {
                LOGGER.info("Could not load properties from path:" + location + ", " + var11.getMessage());
                var11.printStackTrace();
            } finally {
                IOUtils.closeQuietly(reader);
            }
        }

        return props;
    }

    public static void modifyConfig(String key, String value, String cfgFile) {
        try {
            Properties prop = loadProperties(new String[]{cfgFile});
            prop.setProperty(key, value);
            String path = Global.class.getResource(cfgFile).getPath();
            FileOutputStream outputFile = new FileOutputStream(path);
            prop.store(outputFile, "modify");
            outputFile.close();
            outputFile.flush();
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }
}
